package org.example.myleet.p1208;

public class Solution {
    /**
     * 5 ms
     * 双指针法
     */
    public int equalSubstring(String s, String t, int maxCost) {
        int n=s.length(), cost = 0, l=0, r=0, maxLen=0;
        int[] diff = new int[n];
        for (int i=0; i<n; i++) {
            diff[i] = Math.abs(s.charAt(i) - t.charAt(i));
        }
        for (int i=0; i<n; i++) {
            cost += diff[r];
            if (cost <= maxCost) {
                //未超过最大消耗时为有效长度
                maxLen = Math.max(maxLen, r-l+1);
            } else {
                cost -= diff[l];
                ++l;
            }
            ++r;
        }
        return maxLen;
    }
}
